#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
分析测试跳过原因的脚本
"""

import pandas as pd
from pathlib import Path
from tests.utils.excel_reader import ExcelTestDataReader

def analyze_excel_data():
    """分析Excel测试数据"""
    excel_file = Path("tests/data/example_test_cases.xlsx")
    
    print("=== Excel数据分析 ===")
    print(f"Excel文件路径: {excel_file}")
    print(f"文件是否存在: {excel_file.exists()}")
    
    if not excel_file.exists():
        print("Excel文件不存在！")
        return
    
    # 读取Excel数据
    df = pd.read_excel(excel_file)
    print(f"\n总测试用例数: {len(df)}")
    print(f"列名: {df.columns.tolist()}")
    
    # 分析缺失值
    print("\n=== 缺失值分析 ===")
    missing_data = df.isnull().sum()
    for col, count in missing_data.items():
        if count > 0:
            print(f"{col}: {count} 个缺失值")
    
    # 分析每个测试用例的完整性
    print("\n=== 测试用例完整性分析 ===")
    reader = ExcelTestDataReader(str(excel_file))
    
    # 检查POST方法的测试用例
    post_cases = reader.read_test_cases_by_method('POST')
    print(f"\nPOST方法测试用例数: {len(post_cases)}")
    
    for i, case in enumerate(post_cases):
        print(f"\n--- 测试用例 {i+1} ---")
        print(f"ID: {case.get('test_case_id', 'N/A')}")
        print(f"名称: {case.get('test_name', 'N/A')}")
        print(f"方法: {case.get('api_method', 'N/A')}")
        print(f"路径: {case.get('api_path', 'N/A')}")
        print(f"期望状态码: {case.get('expected_status_code', 'N/A')}")
        
        # 检查必填字段
        required_fields = ['test_case_id', 'test_name', 'api_method', 'api_path', 'expected_status_code']
        missing_fields = []
        for field in required_fields:
            if field not in case or pd.isna(case[field]):
                missing_fields.append(field)
        
        if missing_fields:
            print(f"❌ 缺少必填字段: {missing_fields}")
            print(f"❌ 验证结果: {reader.validate_test_case(case)}")
        else:
            print(f"✅ 验证结果: {reader.validate_test_case(case)}")
        
        # 检查请求数据字段
        print(f"用户名: {case.get('name', 'N/A')}")
        print(f"邮箱: {case.get('email', 'N/A')}")
        print(f"年龄: {case.get('age', 'N/A')}")
    
    # 检查GET方法的测试用例
    get_cases = reader.read_test_cases_by_method('GET')
    print(f"\n\nGET方法测试用例数: {len(get_cases)}")
    
    for i, case in enumerate(get_cases):
        print(f"\n--- GET测试用例 {i+1} ---")
        print(f"ID: {case.get('test_case_id', 'N/A')}")
        print(f"名称: {case.get('test_name', 'N/A')}")
        print(f"方法: {case.get('api_method', 'N/A')}")
        print(f"路径: {case.get('api_path', 'N/A')}")
        print(f"期望状态码: {case.get('expected_status_code', 'N/A')}")
        
        # 检查必填字段
        required_fields = ['test_case_id', 'test_name', 'api_method', 'api_path', 'expected_status_code']
        missing_fields = []
        for field in required_fields:
            if field not in case or pd.isna(case[field]):
                missing_fields.append(field)
        
        if missing_fields:
            print(f"❌ 缺少必填字段: {missing_fields}")
            print(f"❌ 验证结果: {reader.validate_test_case(case)}")
        else:
            print(f"✅ 验证结果: {reader.validate_test_case(case)}")

def analyze_skip_reasons():
    """分析测试跳过的具体原因"""
    print("\n\n=== 测试跳过原因分析 ===")
    
    # 从README.md中提到的跳过信息
    print("根据README.md显示的测试结果:")
    print("- 1 passed, 10 skipped")
    print("- 只有1个测试通过，10个被跳过")
    
    print("\n可能的跳过原因:")
    print("1. 测试用例数据不完整 - validate_test_case()返回False")
    print("2. 缺少必填字段: test_case_id, test_name, api_method, api_path, expected_status_code")
    print("3. 字段值为NaN或None")
    
    print("\n解决方案:")
    print("1. 检查Excel文件中的数据完整性")
    print("2. 确保所有必填字段都有值")
    print("3. 修复Excel文件中的缺失数据")
    print("4. 或者修改validate_test_case()方法的验证逻辑")

if __name__ == "__main__":
    analyze_excel_data()
    analyze_skip_reasons()